<template>
  <div class="register-container">

    <div class="regiter-public">
      <el-form :rules="rules">
        <!-- 用户名 -->
        <el-form-item class="form-group">
          <el-input type="text" v-model.trim="formData.username"
                 placeholder="请输入用户名(4-50字符)" required/>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item class="form-group">
          <label>密码<span class="required">*</span></label>
          <el-input type="password" v-model.trim="formData.password"
                 placeholder="请输入密码(8-100字符)" required/>
        </el-form-item>

        <!-- 手机号 -->
        <el-form-item class="form-group">
          <label>手机号<span class="required">*</span></label>
          <el-input type="tel" v-model.trim="formData.phone"
                 placeholder="请输入手机号" pattern="^1[3-9]\d{9}$"/>
        </el-form-item>

        <!-- 邮箱 -->
        <el-form-item class="form-group">
          <label>邮箱</label>
          <el-input type="email" v-model.trim="formData.email"
                 placeholder="请输入邮箱"/>
        </el-form-item>

        <!-- 真实姓名 -->
        <el-form-item class="form-group">
          <label>真实姓名</label>
          <el-input type="text" v-model.trim="formData.realName"
                 placeholder="请输入真实姓名(2-30字符)"/>
        </el-form-item>

        <!-- 身份证号 -->
        <el-form-item class="form-group">
          <label>身份证号</label>
          <el-input type="text" v-model.trim="formData.idCard"
                 placeholder="请输入身份证号(18位)" maxlength="18"/>
        </el-form-item>

        <!-- 头像上传 -->
        <el-form-item class="form-group">
          <label>头像</label>
          <div class="upload-area">
            <img v-if="formData.avatar" :src="formData.avatar" class="avatar-preview">
            <el-input type="file" accept="image/*" @change="handleAvatarUpload"/>
          </div>
        </el-form-item>

        <el-form-item class="form-footer">
          <el-button @click="handleSubmit" :disabled="isSubmitting" class="submit-btn">
            {{ isSubmitting ? '提交中...' : '注册账号' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script setup>
import { ref, reactive ,toRaw} from 'vue'
import {UserApi} from "@/api/user";
import {ElMessage} from "element-plus";
import { useRouter } from 'vue-router'

const router = useRouter()

// 表单数据结构 (完整匹配数据库字段)
const formData = reactive({
  username: '',
  password: '',
  phone: '',
  email: '',
  userType: 1,  // 默认普通用户(按图枚举值)
  realName: '',
  idCard: '',
  avatar: ''    // 存储头像URL
})

const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  phone: [
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  email: [
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  reaName: [
    { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
  ],
  idCard: [
    { pattern: /^\d{18}$/, message: '请输入正确的身份证号', trigger: 'blur' }
  ],
  avatar: [
    { required: false, message: '请上传头像', trigger: 'change' }
  ]
})

// 表单提交状态
const isSubmitting = ref(false)

// 头像上传处理
const handleAvatarUpload = (e) => {
  const file = e.target.files[0]
  if (!file) return

  // 创建预览URL
  formData.avatar = URL.createObjectURL(file)

  // 实际项目需上传到服务器
  const uploadForm = new FormData()
  uploadForm.append('avatar', file)

}

// 提交注册表单
const handleSubmit = async () => {
  console.log('提交表单', formData)
  try {
    // 模拟注册逻辑 (实际项目需替换为API调用)
    const res = await UserApi.registerPublic(toRaw(formData))

    console.log('注册成功', res)

    ElMessage({
      type: 'success',
      message: '注册成功'
    })

    resetForm()

    setTimeout(() => {
      router.push('/login')
    }, 1000)

  } catch (error) {
    console.error('注册失败', error)
    ElMessage({
      type:'error',
      message: error.response?.data?.message || error.message
    })
  }
}

// 重置表单
const resetForm = () => {
  Object.assign(formData, {
    username: '',
    password: '',
    phone: '',
    email: '',
    real_name: '',
    id_card: '',
    avatar: ''
  })
}
</script>

<style scoped>
.register-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.form-group {
  margin-bottom: 20px;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
}

input[type="text"],
input[type="password"],
input[type="tel"],
input[type="email"] {
  width: 100%;
  padding: 10px;
  border: 1px solid #ced4da;
  border-radius: 4px;
  box-sizing: border-box;
}

.required {
  color: #ff4d4f;
  margin-left: 4px;
}

.upload-area {
  border: 1px dashed #d9d9d9;
  padding: 15px;
  text-align: center;
  cursor: pointer;
  border-radius: 4px;
  background: #fafafa;
}

.avatar-preview {
  max-width: 100px;
  max-height: 100px;
  margin-bottom: 10px;
}

.submit-btn {
  background: #1890ff;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.submit-btn:disabled {
  background: #a0d1ff;
  cursor: not-allowed;
}
</style>